package com.tyqx.biz;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tyqx.apiutil.common.GetUrlUtil;
import com.tyqx.apiutil.common.RestTemplateUtil;
import com.tyqx.bean.model.*;
import com.tyqx.service.EthIncomeListService;
import com.tyqx.service.EthPoolInfoService;
import com.tyqx.service.RatesInfoService;
import com.tyqx.service.UserHashrateInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class SchedulerRatesInfoByDay {

    private static final Logger logger = LoggerFactory.getLogger(SchedulerRatesInfoByDay.class);


    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private RatesInfoService ratesInfoService;


    ///@Async
    //表示每隔秒
//    @Scheduled(fixedRate = 60 * 1000 * 10)
    @Scheduled(cron = "0 30 8 * * *")
    public void testTasks() throws Exception {
        logger.debug("获取每天美元兑换人民币的汇率（定时器启动）");
        try {
            Date currentDate = new Date();


            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


            currentDate = dateFormat.parse(dateFormat.format(currentDate));

            String rateApiUrl = "https://api.ratesapi.io/api/latest?base=USD&symbols=CNY";


            String rateInfo = RestTemplateUtil.getUrl(rateApiUrl);

            JSONObject rateJson = JSONObject.parseObject(rateInfo);

            String rateBase = rateJson.getString("base");

            String rateDate = rateJson.getString("date");

            JSONObject rates = rateJson.getJSONObject("rates");

            String rateSymbols = "CNY";

            Double cnyRate = rates.getDouble("CNY");

            if(cnyRate > 0){
                RatesInfo ratesInfo = new RatesInfo();
                ratesInfo.setRates(cnyRate);
                ratesInfo.setCreateDate(currentDate);
                ratesInfo.setRateDate(rateDate);
                ratesInfo.setRateSymbols(rateSymbols);
                ratesInfo.setRateBase(rateBase);

                RatesInfo ratesInfo1 = ratesInfoService.selectRatesInfoByDate();
                if(!rateDate.equals(ratesInfo1.getRateDate())){
                    Integer flag = ratesInfoService.insertRatesInfo(ratesInfo);
                    if (flag > 0)
                        logger.debug("添加每日汇率成功");
                    else
                        logger.error("添加每日汇率失败");
                }else{
                    Integer flag = ratesInfoService.updateRatesInfoByDate(cnyRate,rateDate);
                    if (flag > 0)
                        logger.debug("修改每日汇率成功");
                    else
                        logger.error("修改每日汇率失败");
                }


            }

        } catch (Exception e) {
            e.printStackTrace();
        }



        logger.debug("获取每天美元兑换人民币的汇率（定时器结束）");
    }
}